from __future__ import annotations

import asyncio
from datetime import date, datetime, timezone

from sqlalchemy import select

from app.core.security import get_password_hash
from app.db.session import async_session_factory
from app.models.anniversary import Anniversary
from app.models.cycle import CycleLog, CyclePrediction
from app.models.diary import DiaryEntry
from app.models.relationship import Relationship
from app.models.user import PartnerProfile, User


async def seed() -> None:
  async with async_session_factory() as session:
    existing = await session.scalar(select(User).where(User.email == "demo@love.notes"))
    if existing:
      print("Demo data already exists, skip seeding.")
      return

    user = User(
      email="demo@love.notes",
      username="lovely",
      password_hash=get_password_hash("DemoPass123"),
      is_active=True,
      verified_at=datetime.now(tz=timezone.utc),
    )
    partner = User(
      email="partner@love.notes",
      username="partner",
      password_hash=get_password_hash("DemoPass123"),
      is_active=True,
      verified_at=datetime.now(tz=timezone.utc),
    )
    session.add_all([user, partner])
    await session.flush()

    relationship = Relationship(
      initiator_id=user.id,
      partner_id=partner.id,
      status="active",
      confirmed_at=datetime.now(tz=timezone.utc),
    )
    session.add(relationship)
    await session.flush()

    user.active_relationship_id = relationship.id
    partner.active_relationship_id = relationship.id
    profile = PartnerProfile(
      user_id=user.id,
      partner_name="Daisy",
      relationship_start_date=date(2023, 5, 20),
      timezone="Asia/Shanghai",
      notification_prefs_json={"anniversary_reminder": True},
    )
    session.add(profile)

    diary = DiaryEntry(
      user_id=user.id,
      relationship_id=relationship.id,
      title="周末海边散步",
      content_md="今天和她一起走到海边，看晚霞，感觉彼此脚步都慢下来。",
      mood="幸福",
      tags_json=["海边", "散步"],
      ai_summary="海边散步，晚霞映红幸福心情。",
    )
    session.add(diary)

    anniversary = Anniversary(
      user_id=user.id,
      relationship_id=relationship.id,
      name="第一次牵手",
      date=date(2024, 11, 11),
      recurrence="yearly",
      reminder_offset_days=3,
    )
    session.add(anniversary)

    cycle_log = CycleLog(
      user_id=partner.id,
      relationship_id=relationship.id,
      start_date=date(2024, 12, 1),
      end_date=date(2024, 12, 6),
      symptoms_json={"state": "轻微"},
      notes="需要多喝热水，晚上早点休息。",
    )
    session.add(cycle_log)
    await session.flush()

    prediction = CyclePrediction(
      user_id=partner.id,
      log_id=cycle_log.id,
      relationship_id=relationship.id,
      phase="safe",
      start_date=date(2024, 12, 20),
      end_date=date(2025, 1, 2),
      confidence=0.72,
    )
    session.add(prediction)

    await session.commit()
    print("Demo data inserted. Email: demo@love.notes / partner@love.notes, password: DemoPass123")


if __name__ == "__main__":
  asyncio.run(seed())
